﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using Microsoft.ApplicationBlocks.Data;
using TestSystem.BLL.TestSystem;

namespace SuggestionService
{
    public class DataAccess
    {
        private static readonly string AspNetConnectionString = null;

        private static readonly string TsConnectionString = null;

        static DataAccess()
        {
            AspNetConnectionString = ConfigurationManager.ConnectionStrings["AspNetConnectionString"].ConnectionString;
            TsConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }

        public static List<User> GetAllUsers ()
        {
            IDataReader reader = null;
            var users = new List<User>();
            using (reader = SqlHelper.ExecuteReader(AspNetConnectionString, CommandType.StoredProcedure, "User_GetAll", null))
            {
                while (reader.Read())
                {
                    users.Add(new User() {UserId = reader["UserName"].ToString()});
                }
            }
            return users;
        }

        public static List<User> GetAllUserIds()
        {
            IDataReader reader = null;
            var users = new List<User>();
            using (reader = SqlHelper.ExecuteReader(AspNetConnectionString, CommandType.StoredProcedure, "User_GetAll", null))
            {
                while (reader.Read())
                {
                    users.Add(new User() { UserId = reader["UserId"].ToString() });
                }
            }
            return users;
        }

        public static List<User> GetAllUsersExcept(string userId)
        {
            return GetAllUsers().Where(t => t.UserId != userId).ToList();
        }

        public static List<Quiz> GetAllQuizes()
        {
            var quizes = TestSystem.BLL.TestSystem.Quiz.GetAll();
            return (from quiz in quizes
                    select new Quiz {QuizId = quiz.QuizID, QuizName = quiz.Title}).ToList();
        }

        public static List<QuizTaken> GetQuizesTakenByUser(string userId)
        {
            var result = new List<QuizTaken>();
            result = (List<QuizTaken>)CacheManager.GetFromCache("quizestaken_" + userId);

            if (result == null)
            {
                var quizesTaken = QuizHistory.GetByUserID(userId).ToList();
                var allQuizes = DataAccess.GetAllQuizes();
                result = new List<QuizTaken>();

                for (int i = 0; i < allQuizes.Count; i++)
                {
                    var idx = i;
                    var quizes = quizesTaken.FindAll(q => q.QuizID == allQuizes[idx].QuizId).OrderByDescending(q => q.Scores).ToList();
                    //QuizTaken quiz = quizesTaken.Find(q => q.QuizID == allQuizes[idx].QuizId);
                    QuizHistory quiz = null;
                    if (quizes.Count > 0)
                        quiz = quizes[0];
                    
                    if (quiz != null)
                    {
                        var q = new QuizTaken();
                        q.MaxScores = quiz.QuizTaken.MaxScores;
                        q.Quiz = allQuizes[i];
                        q.Scores = quiz.Scores;
                        result.Add(q);
                    }
                    else
                    {
                        var q = new QuizTaken {Quiz = new Quiz {QuizId = allQuizes[i].QuizId}};
                        q.MaxScores = 100;
                        q.Scores = 25;
                        q.Quiz.QuizName = allQuizes[i].QuizName;
                        result.Add(q);
                    }
                }
                CacheManager.PutIntoCache("quizestaken_" + userId, result, 3600);
            }
            return result;
        }
    }
}